<template>
    <div>
        <el-button type="primary" @click="dialogFormVisible = true">增加</el-button>
        <el-dialog title="增加学生" :visible.sync="dialogFormVisible" @open="handleOpen">
            <el-form :model="form">
                <el-form-item label="上传头像" :label-width="formLabelWidth">
                    <el-upload class="avatar-uploader"
                        :action="url + '/upload'"
                        :show-file-list="false" 
                        :on-success="handleAvatarSuccess" 
                        :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="url + '/images/' + imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄" :label-width="formLabelWidth">
                    <el-input v-model="form.age" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" :label-width="formLabelWidth">
                    <el-radio v-model="form.gender" label="男">男</el-radio>
                    <el-radio v-model="form.gender" label="女">女</el-radio>
                </el-form-item>
                <el-form-item label="选择班级" :label-width="formLabelWidth">
                    <el-select v-model="form.classes" placeholder="请选择">
                        <el-option v-for="item in classAry" :key="item._id" :label="item.name" :value="item._id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleAdd">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { add } from '../../services/student';
import { findAll } from '@/services/class';
import {url} from '../../services';
export default {
    components: {
    },
    data() {
        return {
            form: {
                name: "",
                gender: "",
                age: 0,
                classes: ""
            },
            classAry: [],
            dialogFormVisible: false,
            formLabelWidth: '90px',
            url,
            imageUrl:""
        }
    },
    methods: {
        async handleAdd() {
            await add({...this.form,headImg:this.imageUrl});
            this.dialogFormVisible = false;
            this.$emit('show');

        },
        async handleOpen() {
            console.log("open");
            this.classAry = await findAll();
        },
        handleAvatarSuccess(res, file){
            console.log("res",res);
            this.imageUrl = res.filename;
        }
    }
}
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }
  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
</style>